package com.bltec.emergency.controller.statisticsController;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.ResponseStatistics;
import com.bltec.emergency.service.statisticsService.ResponseStatisticsService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/responseStatistics")
@RequiresPermissions("project:statistics")
public class ResponseStatisticsController {
    @Resource
    private ResponseStatisticsService responseStatisticsService;
    @Resource
    private UserService userService;

    @RequestMapping("getAllResponseStatistics")
    public ResponseJson getAllResponseStatistics() {
        try {
            List<ResponseStatistics> responseStatistics = responseStatisticsService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(responseStatistics));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /*    private String materialCategory;
        private Long departmentId;
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date time;
        private String type;*/
    @RequiresPermissions("statistics:insert")
    @RequestMapping("addResponseStatistics")
    public ResponseJson addResponseStatistics(String materialCategory, Long departmentId, Date time, String type) {
        try {
            ResponseStatistics responseStatistics = new ResponseStatistics();
            responseStatistics.setMaterialCategory(materialCategory);
            responseStatistics.setDepartmentId(departmentId);
            responseStatistics.setTime(time);
            responseStatistics.setType(type);
            responseStatistics.setEnable(true);
            responseStatistics.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            responseStatistics.setCreateTime(DateUtil.dateNow());
            responseStatisticsService.add(responseStatistics);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }

    @RequiresPermissions("statistics:update")
    @RequestMapping("updateResponseStatistics")
    public ResponseJson updateResponseStatistics(Long id, String materialCategory, Long departmentId, Date time, String type) {
        try {
            ResponseStatistics responseStatistics = responseStatisticsService.getResponseStatisticsById(id);
            responseStatistics.setMaterialCategory(materialCategory);
            responseStatistics.setDepartmentId(departmentId);
            responseStatistics.setTime(time);
            responseStatistics.setType(type);
            responseStatistics.setEnable(true);
            responseStatistics.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            responseStatistics.setUpdateTime(DateUtil.dateNow());
            responseStatisticsService.updateResponseStatisticsById(responseStatistics);
            return new ResponseJson(Constant.OK, JSON.toJSON(responseStatistics));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }
    @RequiresPermissions("statistics:delete")
    @RequestMapping("deleteResponseStatistics")
    public ResponseJson setDisable(Long id) {
        if (null != responseStatisticsService.getResponseStatisticsById(id)) {
            if (responseStatisticsService.deleteResponseStatisticsById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }
}

